﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace QXBook.Utils
{
    public class CachingService
    {
        public static string Key = "QXBook_System_";
        public static HttpContext HttpContext
        {
            get
            {
                return CachingService._ihttpcontext;
            }
            set
            {
                CachingService._ihttpcontext = value;
            }
        }
        private static HttpContext _ihttpcontext = null;

        public static object Get(string name)
        {
            if (CachingService.HttpContext != null)
            {
                return CachingService.HttpContext.Cache.Get(CachingService.Key + name);
            }
            else
            {
                return HttpContext.Current.Cache.Get(CachingService.Key + name);
            }
        }

        public static void Remove(string name)
        {
            if (CachingService.HttpContext != null)
            {
                if (CachingService.HttpContext.Cache[CachingService.Key + name] != null)
                {
                    CachingService.HttpContext.Cache.Remove(CachingService.Key + name);
                }
            }
            else
            {
                if (HttpContext.Current.Cache[CachingService.Key + name] != null)
                {
                    HttpContext.Current.Cache.Remove(CachingService.Key + name);
                }
            }
        }

        public static void Set(string name, object value, CacheDependency cacheDependency)
        {
            if (value == null) return;
            if (CachingService.HttpContext != null)
            {
                CachingService.HttpContext.Cache.Insert(CachingService.Key + name, value, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
            }
            else
            {
                HttpContext.Current.Cache.Insert(CachingService.Key + name, value, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
            }
        }

        public static void SetCache(string name, object value, CacheDependency dependency, int expireTime)
        {
            if (CachingService.HttpContext != null)
            {
                CachingService.HttpContext.Cache.Insert(CachingService.Key + name, value, dependency, DateTime.Now.AddMinutes((double)expireTime), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            }
            else
            {
                HttpContext.Current.Cache.Insert(CachingService.Key + name, value, dependency, DateTime.Now.AddMinutes((double)expireTime), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            }


        }

        #region 系统Caching参数定义
        /// <summary>
        /// 所有模版信息缓存
        /// </summary>
        public static string CACHING_All_TEMPLATES = "CACHING_All_TEMPLATES";
        /// <summary>
        /// 所有模板实体
        /// </summary>
        public static string CACHING_All_TEMPLATES_ENTITY = "CACHING_All_TEMPLATES_ENTITY";
        /// <summary>
        /// 所有系统模版信息
        /// </summary>
        public static string CACHING_All_SYSTEM_TEMPLATES = "CACHING_All_SYSTEM_TEMPLATES";
        /// <summary>
        /// 所有的资源分类信息DATATABle
        /// </summary>
        public static string CACHING_ALL_CATEGORIES = "CACHING_ALL_CATEGORIES";
        /// <summary>
        /// 所有的资源分类属性信息DATATABle
        /// </summary>
        public static string CACHING_ALL_PROPERTIES = "CACHING_ALL_PROPERTIES";
        /// <summary>
        /// 所有的资源分类属性信息实体
        /// </summary>
        public static string CACHING_ALL_PROPERTIES_ENTITY = "CACHING_ALL_PROPERTIES_ENTITY";
        /// <summary>
        /// 所有的资源分类信息实体
        /// </summary>
        public static string CACHING_ALL_CATEGORIES_ENTITY = "CACHING_ALL_CATEGORIES_ENTITY";
        /// <summary>
        /// 所有权限项实体
        /// </summary>
        public static string CACHING_ALL_POPDOM = "CACHING_ALL_POPDOM";
        /// <summary>
        /// 所有的管理员实体
        /// </summary>
        public static string CACHING_ALL_ADMIN_ENTITY = "CACHING_ALL_ADMIN_ENTITY";
        /// <summary>
        /// 所有权限组实体
        /// </summary>
        public static string CACHING_ALL_ADMINROLE_ENTITY = "CACHING_ALL_ADMINROLE_ENTITY";
        /// <summary>
        /// 所有图片分类实体
        /// </summary>
        public static string CACHING_ALL_FILECATEGORIES_ENTITY = "CACHING_ALL_FILECATEGORIES_ENTITY";

        /// <summary>
        /// 图片实体单个缓存
        /// </summary>
        public static string CACHING_FILECATEGORIES_ENTITY = "CACHING_FILECATEGORIES_ENTITY_";

        public static string CACHING_DEFAULTSKIN_CATEGROIES_ENTITY = "CACHINGDEFAULTSKINCATEGROIESENTITY";

        public static string CACHING_SHEIF_CATEGORIES_CONFIG = "CACHINGSHEIFCATEGORIESCONFIG";

        public static string CACHING_ALL_SHEIFSOURCE_ENTITY = "CACHINGALLSHEIFSOURCEENTITY";
        /// <summary>
        /// 分类属性缓存
        /// </summary>
        public static string CACHING_ALL_PROPERTIES_CATEGORIES_ENTITY = "CACHING_ALL_PROPERTIES_CATEGORIES_ENTITY";
        public static string CACHING_ALL_PROPERTIES_CATEGORIES = "CACHING_ALL_PROPERTIES_CATEGORIES";

        public static string CACHING_ALL_SPECIALITY_ENTITY = "CACHING_ALL_SPECIALITY_ENTITY";
        /// <summary>
        /// 系统缓存集合
        /// </summary>
        public static Dictionary<string, string> SystemCachings
        {
            get
            {
                if (_systemcachings == null)
                {
                    _systemcachings = new Dictionary<string, string>();
                    _systemcachings.Add("所有模版信息缓存", CACHING_All_TEMPLATES);
                    _systemcachings.Add("所有模板实体", CACHING_All_TEMPLATES_ENTITY);
                    _systemcachings.Add("所有系统模版信息", CACHING_All_SYSTEM_TEMPLATES);
                    _systemcachings.Add("所有的资源分类信息", CACHING_ALL_CATEGORIES);
                    _systemcachings.Add("所有的资源分类信息实体", CACHING_ALL_CATEGORIES_ENTITY);
                    _systemcachings.Add("所有模板实体", CACHING_All_TEMPLATES_ENTITY);
                    _systemcachings.Add("所有权限项实体", CACHING_ALL_POPDOM);
                    _systemcachings.Add("所有的管理员实体", CACHING_ALL_ADMIN_ENTITY);
                    _systemcachings.Add("所有权限组实体", CACHING_ALL_ADMINROLE_ENTITY);
                    _systemcachings.Add("所有图片分类实体", CACHING_ALL_FILECATEGORIES_ENTITY);
                }
                return _systemcachings;
            }
        }
        private static Dictionary<string, string> _systemcachings = null;
        #endregion
    }
}
